package grails.goods.goods

import grails.goods.base.BaseService
import grails.gorm.transactions.Transactional
import groovy.sql.Sql

@Transactional
class GoodsService extends BaseService {

    def getList() {
        def list = Goods.list()
        def totalCount = list.size()
        return [list: list, totalCount: totalCount]
    }

    def getPage(def param) {
        def result = [result: true]

        def sql = new StringBuffer("""
            SELECT 
            SQL_CALC_FOUND_ROWS
              id,
              desciption,
              price,
              inventory,
              goodsname,
              `status`
            FROM `goods`
            WHERE `status` != -1 
            AND 1=1 
        """)
        if (param.goodsname != null) {
            sql.append(" AND goodsname like'%${param.goodsname}%' ")
        }
        if (param.maxPrice) {
            sql.append(" AND price<=${param.maxPrice} ")
        }
        if (param.minPrice) {
            sql.append(" AND price >=${param.minPrice} ")
        }
        sql.append(" LIMIT ${getStartNum(param.pageIndex, param.pageSize)},${param.pageSize} ")

        //执行sql查询数据
        def db = new Sql(sessionFactory.currentSession.connection())
//      def db = new Sql(sessionFactory.currentSession.connection())
        def list = db.rows(sql.toString())
        def count = db.rows(' SELECT FOUND_ROWS() AS `count` ')[0].count
        def data = [:]
        if (param.pageSize) {
            data.totalPage = getTotalPage(param.pageSize, count as Integer)
            data.pageSize = param.pageSize
        }
        data.pageIndex = param.pageIndex
        data.list = list
        data.count = count
        result.data = data
        return result
    }


    def addGoods(def param) {
        def result = [result: true, msg: '保存成功']
        def goods = new Goods()
        goods.setGoodsname(param.goodsname)
        goods.setDesciption(param.desciption)
        goods.setPrice(param.price)
        goods.setInventory(param.inventory)
        goods.setStatus(param.status)
        println param.goodsname
        def saveResult = goods.save()
        if (!saveResult) {
            return [result: false, msg: '保存失败']
        }
        return result
    }

    def getGoodsById(def param) {
        def result = [result: true]
//        def sql = new String("""
//            SELECT
//            id,
//            desciption,
//            price,
//            inventory,
//            goodsname,
//            `status`
//            FROM `goods`
//            WHERE id = ${param.id}
//        """)
//        def db = new Sql(sessionFactory.currentSession.connection())
//        def list = db.rows(sql)
//        if (!list) {
//            return [result: false, msg: '没有找到指定id的商品信息', data: null]
//        }
        def goods = Goods.get(param.id)
        if (!goods) {
            return [result: true, msg: '没有找到指定id的商品信息']
        }
        result.msg = 'success'
        result.data = goods
        return result
    }

    def updateGoods(def param) {
        def result = [result: true, msg: '修改成功']
        def goods = Goods.get(param.id)
        goods.setGoodsname(param.goodsname)
        goods.setDesciption(param.desciption)
        goods.setPrice(param.price)
        goods.setInventory(param.inventory)
        goods.setStatus(param.status)
        def updateResult = goods.save(flush: true)
        if (!updateResult) {
            return [result: false, msg: '修改失败']
        }
        return result
    }

    def deleteGoodsById(def param) {
        def result = [result: true, msg: '删除成功']
        def goods = Goods.get(param.id)
        goods.status = -1
        goods.save(flush: true)
        return result
    }

    def serviceMethod() {

    }
}
